package com.module.ble.widget

import android.content.Context
import android.graphics.*
import android.util.AttributeSet
import android.view.View
import com.common.base.utils.ViewUtils
import com.module.ble.data.bean.CusChartData

/**
 *
 * 作者：sosou
 *
 * 版本：1.0
 *
 * 创建日期：8/7/2025
 *
 * 描述：
 * 多色分段圆环图控件
 * 根据CusChartData数组的mColor和mProportion绘制圆环
 * 支持自定义圆环宽度
 *
 * 修订历史：
 *
 */
class CusPieChartView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr) {

    // 绘制画笔
    private val mPaint: Paint = Paint().apply {
        style = Paint.Style.STROKE
//        strokeCap = Paint.Cap.ROUND
        isAntiAlias = true
        isDither = true
    }

    // 背景圆环画笔
    private val mBackgroundPaint: Paint = Paint().apply {
        style = Paint.Style.STROKE
//        strokeCap = Paint.Cap.ROUND
        isAntiAlias = true
        isDither = true
        color = Color.parseColor("#F5F5F5")
    }

    // 绘制区域
    private var mRectF: RectF? = null

    // 数据源
    private var mDataList: List<CusChartData> = emptyList()

    // 圆环宽度
    private var mStrokeWidth: Float = ViewUtils.dp2px(14f)
        set(value) {
            field = value
            mPaint.strokeWidth = value
            mBackgroundPaint.strokeWidth = value
            invalidate()
        }

    // 是否显示背景圆环
    private var mShowBackground: Boolean = true
        set(value) {
            field = value
            invalidate()
        }

    // 起始角度（-90度表示从顶部开始）
    private var mStartAngle: Float = -90f
        set(value) {
            field = value
            invalidate()
        }

    init {
        mPaint.strokeWidth = mStrokeWidth
        mBackgroundPaint.strokeWidth = mStrokeWidth
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)

        val viewWidth = measuredWidth - paddingLeft - paddingRight
        val viewHeight = measuredHeight - paddingTop - paddingBottom
        val size = minOf(viewWidth, viewHeight)

        // 计算绘制区域，考虑画笔宽度
        val halfStroke = mStrokeWidth / 2
        val left = paddingLeft + (viewWidth - size) / 2 + halfStroke
        val top = paddingTop + (viewHeight - size) / 2 + halfStroke
        val right = left + size - mStrokeWidth
        val bottom = top + size - mStrokeWidth

        mRectF = RectF(left, top, right, bottom)
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)

        mRectF?.let { rectF ->
            // 绘制背景圆环
            if (mShowBackground) {
                canvas.drawArc(rectF, 0f, 360f, false, mBackgroundPaint)
            }

            // 绘制数据圆环
            drawDataArcs(canvas, rectF)
        }
    }

    private fun drawDataArcs(canvas: Canvas, rectF: RectF) {
        if (mDataList.isEmpty()) return

        // 计算总比例
        val totalProportion = mDataList.sumOf { it.mProportion.toDouble() }.toFloat()
        if (totalProportion <= 0) return

        var currentAngle = mStartAngle

        mDataList.forEach { data ->
            if (data.mProportion > 0) {
                // 计算当前扇形的角度
                val sweepAngle = (data.mProportion / totalProportion) * 360f

                // 设置画笔颜色
                mPaint.color = data.mColor

                // 绘制扇形
                canvas.drawArc(rectF, currentAngle, sweepAngle, false, mPaint)

                // 更新角度
                currentAngle += sweepAngle
            }
        }
    }

    /**
     * 设置数据
     * @param dataList CusChartData列表，使用mColor和mProportion属性
     */
    fun setData(dataList: List<CusChartData>) {
        mDataList = dataList
        invalidate()
    }

    /**
     * 获取当前数据
     */
    fun getData(): List<CusChartData> {
        return mDataList
    }

    /**
     * 设置圆环宽度
     * @param width 圆环宽度，单位px
     */
    fun setStrokeWidth(width: Float) {
        mStrokeWidth = width
    }

    /**
     * 获取圆环宽度
     */
    fun getStrokeWidth(): Float {
        return mStrokeWidth
    }

    /**
     * 设置是否显示背景圆环
     * @param show 是否显示
     */
    fun setShowBackground(show: Boolean) {
        mShowBackground = show
    }

//    /**
//     * 设置背景圆环颜色
//     * @param color 颜色值
//     */
//    fun setBackgroundColor(color: Int) {
//        mBackgroundPaint.color = color
//        invalidate()
//    }

    /**
     * 设置起始角度
     * @param angle 角度，-90表示从顶部开始，0表示从右侧开始
     */
    fun setStartAngle(angle: Float) {
        mStartAngle = angle
    }

    /**
     * 获取起始角度
     */
    fun getStartAngle(): Float {
        return mStartAngle
    }

    /**
     * 清空数据
     */
    fun clearData() {
        mDataList = emptyList()
        invalidate()
    }
}